<?php
namespace app\common\component\user_activation_log;

use app\common\model\UserActivationLog;
use app\common\component\user_activation\UserActivationService;

class UserActivationLogService {
	public function log($type, $active, $price, $to_user_name, $from_user_name, $remark, $is_state, $voucher = '') {
		$resultUser = \app\common\component\user\UserModel::get(['username' => $to_user_name]);

		if($resultUser) {
			$data = array();
			$data['type'] = $type;
			$data['active'] = $active;
			$data['price'] = $price;
			$data['to_user_name'] = $to_user_name;
			$data['from_user_name'] = $from_user_name;

			$resultGetUserActivation = (new UserActivationService) -> getUserActivationId($resultUser['user_id']);
			$data['to_user_name_old_price'] = $resultGetUserActivation['data']['user_activation'] -> price;
			switch($active) {
				case 0:
					$data['to_user_name_new_price'] = $resultGetUserActivation['data']['user_activation'] -> price - $price;
					break;
				case 1:
					$data['to_user_name_new_price'] = $resultGetUserActivation['data']['user_activation'] -> price + $price;
					break;
			}

			$data['create_time'] = time();
			$data['voucher'] = $voucher;
			$data['remark'] = $remark;
			$data['is_state'] = $is_state;

			return $this -> create($data);
		}

		return [
			'code' => -1,
			'message' => '用户不存在'
		];
	}

	public function create($data) {
		$UserActivationLog = new UserActivationLog($data);

    	if($UserActivationLog -> save()) {
    		return [
    			'code' => 0,
    			'message' => '激活码日志创建成功!',
    			'data' => [
    				'user_wallet_log' => $UserActivationLog
    			]
    		];
    	}

    	return [
			'code' => -1,
			'message' => '激活码日志创建失败!'
		];
	}
}
?>